package com.tjsinfo.tjsoa.worklog.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.common.PageController;
import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.worklog.bo.IWorkLogExamineBo;
public class CheckSuddenlyWorkLogAction extends Action{
	private IWorkLogExamineBo workLogExamineBo;
	
	public IWorkLogExamineBo getWorkLogExamineBo() {
		return workLogExamineBo;
	}

	public void setWorkLogExamineBo(IWorkLogExamineBo workLogExamineBo) {
		this.workLogExamineBo = workLogExamineBo;
	}

	/* (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception { 
 		// TODO Auto-generated method stub
		try {
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("user");
			List list =	workLogExamineBo.selectSuddenlyWorkLog(user.getUser_id(), 0);
			Integer contentspage = null;
			String start = null;
			 PageController pageController = new PageController();
			    if(request.getParameter("page") != null && request.getParameter("start") == null )
				{
					pageController.setPageController(list);
					pageController.setMaxPageRows(18);
					list = (ArrayList) pageController.getPageContents(Integer.parseInt(request.getParameter("page") ));
			
				}
			    else if (request.getParameter("page") != null) {
					contentspage = Integer.parseInt(request.getParameter("page"));
					start = request.getParameter("start");
					pageController.setPageController(list);
					pageController.setMaxPageRows(18);
					if (start.equals("up")) {
						list = (ArrayList) pageController
								.getPageContents(contentspage - 1);
					} else if(start.equals("down")) {
						list = (ArrayList) pageController
								.getPageContents(contentspage + 1);
					}
					else if(start.equals("first"))
					{
						list = (ArrayList) pageController
						.getPageContents(1);
					}
					else if(start.equals("last"))
					{
						list = (ArrayList) pageController
						.getPageContents(pageController.getPageCount());
					}
				} else {
					pageController.setPageController(list);
					pageController.setMaxPageRows(18);
					list = (ArrayList) pageController.getPageContents(1);
				}
			request.setAttribute("list", list);
			request.setAttribute("pageController",pageController);
			return mapping.findForward("ok");
		
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return mapping.findForward("error");
		}
		
	}
	
}
